package com.xjgc.sap.ws.service;

import com.xjgc.common.config.DbConn;
import com.xjgc.common.utils.SpringContextUtils;
import com.xjgc.sap.ws.client.ZXJMESBACK;
import com.xjgc.sap.ws.dao.ZxjM2Scprk;
import com.xjgc.sap.ws.dao.ZxjM2Sfhhc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Mes系统传输MSI数据
 * @author Administrator
 *
 */
@Service
public class ZxjMes2SapService {

	@Autowired
	DbConn db;
	@Autowired
	ZxjM2Scprk zxjM2Scprk;
	@Autowired
	ZxjM2Sfhhc zxjM2Sfhhc;//发货回传信息

	public void getData() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		
		//初始化spring的上下文
		ZXJMESBACK  msiToSap = (ZXJMESBACK) SpringContextUtils.getBean("ZXJ_MES_BACK");
				
		//成品入库信息
		String cprkXml = zxjM2Scprk.cprk(db);
		String cprk = null;
		if (cprkXml != null) {
			
			try {
				cprk = msiToSap.zxjMesBack("ZXJ_M2S_CPRK","CPRK"+sdf.format(new Date()),cprkXml,"");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(("1").equals(cprk)) {//SAP系统接收失败(1:失败，0：成功)
				zxjM2Scprk.eUpdate(cprkXml,db);
			}
		}
		
		//发货回传信息
		String fhhcXml = zxjM2Sfhhc.fhhc(db);
		String fhhc = null;
		
		if (fhhcXml != null) {
			try {
				fhhc = msiToSap.zxjMesBack("ZXJ_M2S_FHHC","FHHC"+sdf.format(new Date()),fhhcXml,"");
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(("1").equals(fhhc)) {//SAP系统接收失败(1:失败，0：成功)
				zxjM2Scprk.eUpdate(cprkXml,db);
			}
		}
	}
}
